{
 "cells": [
  {
   "cell_type": "raw",
   "metadata": {},
   "source": [
    "使用说明：\n",
    "选中下面一行代码，点击👆运行，在输入框内输入地址比如：20211022即可\n",
    "后期更新，自动处理前5次观测数据\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "登陆成功，若报错，请用文件夹进入一次\\10.153.110.37\n",
      "请输出需要整理的日期，比如：20211011CAK,整理文件将放在当日文件夹下20210206\n",
      "你选择的日期是20210206\n",
      "目标地址：\\\\10.153.110.37\\Data\\solar_observation\\2021\\CAK\\20210206CAK\n",
      "<bound method NDFrame.head of                      观测时间段1             观测时间段2  总观测时间  观测文件     文件总大小  \\\n",
      "日期                                                                      \n",
      "20210206  09:08:19-10:36:05  14:54:39-14:43:09  25.27   372  5953.61M   \n",
      "\n",
      "               编号                   方位 黑子数量 耀斑爆发等级和时间  \n",
      "日期                                                     \n",
      "20210206  [12801]  [N30W91(841\",481\")]  [/]       [-]  >\n",
      "已完成，文件放置日期处理目录：\\\\10.153.110.37\\Data\\solar_observation\\2021\\CAK\\20210206CAK\n",
      "数据已保存至：\\\\10.153.110.37\\Data\\solar_observation\\2021\\CAK\\\n",
      "**************************************************\n",
      "目标地址：\\\\10.153.110.37\\Data\\solar_observation\\2021\\WL\\20210206WL\n",
      "<bound method NDFrame.head of                      观测时间段1             观测时间段2  总观测时间  观测文件     文件总大小  \\\n",
      "日期                                                                      \n",
      "20210206  09:03:01-11:59:52  14:21:32-17:54:40    6.5   351  5617.52M   \n",
      "\n",
      "               编号                   方位 黑子数量 耀斑爆发等级和时间  \n",
      "日期                                                     \n",
      "20210206  [12801]  [N30W91(841\",481\")]  [/]       [-]  >\n",
      "已完成，文件放置日期处理目录：\\\\10.153.110.37\\Data\\solar_observation\\2021\\WL\\20210206WL\n",
      "数据已保存至：\\\\10.153.110.37\\Data\\solar_observation\\2021\\WL\n",
      "**************************************************\n"
     ]
    }
   ],
   "source": [
    "import re\n",
    "import os,datetime\n",
    "import datetime as dt\n",
    "import pandas as pd\n",
    "import requests\n",
    "from bs4 import BeautifulSoup\n",
    "import paramiko\n",
    "\n",
    "# 建立一个sshclient对象\n",
    "ssh = paramiko.SSHClient()\n",
    "# 允许将信任的主机自动加入到host_allow 列表，此方法必须放在connect方法的前面\n",
    "ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())\n",
    "# 调用connect方法连接服务器\n",
    "# 需要管理员账号\n",
    "ssh.connect(hostname='10.153.110.37', port=22, username='admin', password='admin')\n",
    "# 执行命令\n",
    "# 关机命令\n",
    "#command=\"shutdown -h now\"\n",
    "#stdin, stdout, stderr = ssh.exec_command(command)\n",
    "# 结果放到stdout中，如果有错误将放到stderr中\n",
    "#print(stdout.read().decode())\n",
    "print('登陆成功，若报错，请用文件夹进入一次\\\\10.153.110.37')\n",
    "os.chdir(r'\\\\10.153.110.37\\Data\\solar_observation\\2021')\n",
    "\n",
    "#如果观测时间分上下午，上午时间一个列表，下午时间一个列表，分开求和再相加得出总的时间\n",
    "def 文件夹日期(path):\n",
    "    return re.search('\\d+',path).group()\n",
    "\n",
    "def 时间计算(time):\n",
    "    if time != []:\n",
    "        a = time[-1]\n",
    "        b = time[0]\n",
    "        c = round((a-b).seconds/60/60,2)\n",
    "        return round(c,2)\n",
    "    else:\n",
    "        return 0\n",
    "def 时间段(time):\n",
    "    if time != []:\n",
    "        b=time[0]\n",
    "        c=time[-1]\n",
    "        return '-'.join((b.strftime('%X'),c.strftime('%X')))\n",
    "    else:\n",
    "        return None\n",
    "    \n",
    "    \n",
    "#爬取太阳黑子，网站https://www.solarmonitor.org/index.php?date=20211023\n",
    "def 黑子(num):\n",
    "    res = requests.get('https://www.solarmonitor.org/index.php?date='+str(num))\n",
    "    bs = BeautifulSoup(res.text,'html.parser')\n",
    "    noaa=bs.find_all('tr',class_='noaaresults')\n",
    "    if noaa ==[]:\n",
    "        #编号\n",
    "        编号.append('平静')\n",
    "        #方位\n",
    "        方位.append('平静')\n",
    "        #黑子数量\n",
    "        黑子数量.append('无黑子')\n",
    "        #positions = position.replace('S','+').replace('N','-')   #需求要把南北换成 +\\-\n",
    "        #耀斑爆发等级和时间\n",
    "        耀斑爆发等级和时间.append('平静')\n",
    "    else:\n",
    "        a=[]\n",
    "        b=[]\n",
    "        c=[]\n",
    "        d=[]\n",
    "        for s in noaa:\n",
    "            #编号\n",
    "            a.append(s.find(id='noaa_number').text.replace(' ',''))\n",
    "            #方位\n",
    "            b.append(s.find(id='position').text.replace(' ',''))\n",
    "            #黑子数量\n",
    "            c.append(s.find(id='nspots').text.replace(' ',''))\n",
    "            #positions = position.replace('S','+').replace('N','-')   #需求要把南北换成 +\\-\n",
    "            #耀斑爆发等级和时间\n",
    "            d.append(s.find(id='events').text.replace(' ',''))\n",
    "        编号.append(a)\n",
    "        方位.append(b)\n",
    "        黑子数量.append(c)\n",
    "        耀斑爆发等级和时间.append(d)\n",
    "        \n",
    "        \n",
    "def 合并():\n",
    "    file37 = ppath1+'\\文件整理表格.xls'\n",
    "    df37 = pd.read_excel(file37)\n",
    "    fileday = ppath+'\\文件整理表格.xls'\n",
    "    df2 = pd.read_excel(fileday)\n",
    "    #df.set_index('日期',inplace = True)\n",
    "    df37=pd.concat([df37,df2])\n",
    "    df37.set_index('日期',inplace = True)\n",
    "    df37.to_excel(ppath1+'\\文件整理表格.xls')\n",
    "    print('数据已保存至：'+ ppath[:-11])\n",
    "    print('*'*50)\n",
    "\n",
    "#表格抬头 Z是字典\n",
    "#num =日期\n",
    "def all(num):\n",
    "    #os.chdir(r'E:\\shuju\\num')\n",
    "    os.chdir(ppath)\n",
    "    mtime = [] #存放上午时间段\n",
    "    ntime = []#存放下午时间段\n",
    "    size = 0 #文件大小\n",
    "    lensize = 0#文件数量\n",
    "    for i in os.scandir(): #单个文件夹资料\n",
    "        if i.name.endswith('.fit'):\n",
    "            #文件大小\n",
    "            size += os.path.getsize(i)/1024/1024\n",
    "            #文件数量\n",
    "            lensize +=1\n",
    "            #print(str('%.2f' %size)+'M')\n",
    "            #print(os.path.getctime(i))\n",
    "            #dt.datetime.fromtimestamp(os.path.getmtime(i).append(mtime)\n",
    "\n",
    "            #创建时间循环\n",
    "            mmtime=dt.datetime.fromtimestamp(os.path.getmtime(i))\n",
    "            if mmtime.hour < 13:   #小于13时的时间放入mmtime列表内\n",
    "                mtime.append(mmtime)\n",
    "            elif 13 < mmtime.hour < 19: #大于13时小于19时的时间放入nmtime列表内\n",
    "                ntime.append(mmtime)\n",
    "    #输出需要的资料，并打包\n",
    "    #print(os.path.split(os.getcwd())[1][:-3])\n",
    "    日期.append(文件夹日期(os.path.split(os.getcwd())[1]))\n",
    "    #print(时间段(mtime))\n",
    "    观测时间段1.append(时间段(mtime))\n",
    "    #print(时间段(ntime))\n",
    "    观测时间段2.append(时间段(ntime))\n",
    "    #print(时间计算(mtime)+时间计算(ntime))\n",
    "    总观测时间.append(时间计算(mtime)+时间计算(ntime))\n",
    "    #print(lensize)\n",
    "    观测文件.append(lensize)\n",
    "    #print('文件总大小'+str('%.2f' %size)+'M')\n",
    "    文件总大小.append(str('%.2f' %size)+'M')\n",
    "    黑子(re.search('\\d+',num).group())\n",
    "    \n",
    "#在ppath填入CAK/WL/HA路径\n",
    "day = input('请输出需要整理的日期，比如：20211011CAK,整理文件将放在当日文件夹下')\n",
    "print('你选择的日期是'+day)\n",
    "wangpan =['CAK','WL']\n",
    "for path37 in wangpan:\n",
    "    fname=[]\n",
    "    日期=[]\n",
    "    观测时间段1=[]\n",
    "    观测时间段2=[]\n",
    "    总观测时间=[]\n",
    "    观测文件=[]\n",
    "    文件总大小=[]\n",
    "    编号=[]\n",
    "    方位=[]\n",
    "    黑子数量=[]\n",
    "    耀斑爆发等级和时间=[]\n",
    "    z={'日期':日期,'观测时间段1':观测时间段1,'观测时间段2':观测时间段2,'总观测时间':总观测时间,'观测文件':观测文件,'文件总大小':文件总大小,'编号':编号,'方位':方位,'黑子数量':黑子数量,'耀斑爆发等级和时间':耀斑爆发等级和时间}\n",
    "    try:\n",
    "        if path37 == 'CAK':\n",
    "            ppath=os.path.join(r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\CAK',day+'CAK')\n",
    "            ppath1=ppath[:-11]\n",
    "        elif path37 == 'WL':\n",
    "            ppath=os.path.join(r'\\\\10.153.110.37\\Data\\solar_observation\\2021\\WL',day+'WL')\n",
    "            ppath1=ppath[:-10]\n",
    "        elif path37 =='HA':\n",
    "            print('暂未开通HA处理')\n",
    "        os.chdir(ppath)\n",
    "        print('目标地址：'+ppath)\n",
    "    \n",
    "        #c处理的最终主程序\n",
    "        all(day)\n",
    "        #处理detafram\n",
    "        df = pd.DataFrame.from_dict(z)\n",
    "        df.set_index('日期',inplace = True)\n",
    "        print(df.head)\n",
    "        #更改文件存放位置\n",
    "        os.chdir(ppath)\n",
    "        df.to_excel(ppath+'\\文件整理表格.xls')\n",
    "        df.drop(df.index,inplace=True)\n",
    "        print('已完成，文件放置日期处理目录：'+ppath)\n",
    "        合并() #合并数据\n",
    "    except FileNotFoundError:\n",
    "        print(path37+'目录下没有当天日期')\n",
    "        pass"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# day = input('请输出需要整理的日期，比如：20201011CAK,整理文件将放在当日文件夹下')\n",
    "# print('你选择的日期是'+day)\n",
    "\n",
    "# wangpan =['CAK','WL']\n",
    "# for path37 in wangpan:\n",
    "#     if path37 == 'CAK':\n",
    "#         ppath=os.path.join(r'\\\\10.153.110.37\\Data\\太阳观测\\2020\\CAK',day)\n",
    "#     elif path37 == 'WL':\n",
    "#         ppath=os.path.join(r'\\\\10.153.110.37\\Data\\太阳观测\\2020\\WL',day)\n",
    "#     elif path37 =='HA':\n",
    "#         print('暂未开通HA处理')\n",
    "# os.chdir(ppath)\n",
    "# print('目标地址：'+ppath)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "history"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "ppath[:-11]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "def 合并():\n",
    "    file37 = r'\\\\10.153.110.37\\Data\\solar_observation\\2020\\WL\\文件整理表格.xls'\n",
    "    df37 = pd.read_excel(file37)\n",
    "    fileday = ppath+'\\文件整理表格.xls'\n",
    "    df2 = pd.read_excel(fileday)\n",
    "    #df.set_index('日期',inplace = True)\n",
    "    df37=pd.concat([df37,df2])\n",
    "    df37.set_index('日期',inplace = True)\n",
    "    df37.to_excel(ppath1+'\\文件整理表格.xls')\n",
    "    print('数据已保存至：'+ ppath[:-11])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "df37"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
